package cn.itcast.bos.web.action;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import cn.itcast.bos.domain.User;
import cn.itcast.bos.service.IUserService;
import cn.itcast.bos.web.action.base.BaseAction;

@Controller
@Scope(value="prototype")
public class UserAction extends BaseAction<User>{
	@Resource
	private IUserService userService;
	
	private String checkcode;
	
	public void setCheckcode(String checkcode) {
		this.checkcode = checkcode;
	}

	public String login(){
		String validateCode = (String) ServletActionContext.getRequest().getSession().getAttribute("key");
		if(StringUtils.isBlank(validateCode) || !checkcode.equals(validateCode)){
			this.addActionError(this.getText("checkCodeError"));
			return "login";
		}else{
			User user = userService.login(model);
			if(user != null){
				ServletActionContext.getRequest().getSession().setAttribute("loginUser", user);
				return "home";
			}else{
				this.addActionError(this.getText("loginFail"));
				return "login";
			}
		}
	}
	
	public String logout(){
		ServletActionContext.getRequest().getSession().invalidate();
		return "login";
	}
}
